import { app, BrowserWindow, Menu, nativeImage, Tray } from "electron";
import * as path from "path";

let iconTray: any = null;

const initTray = () => {
	iconTray = new Tray(path.join(__static, './favicon.ico'))

//绑定右键菜单
	const trayMenu = Menu.buildFromTemplate([
		{
			label: '设置',
			click: function () {
				console.log('setting')
			}
		},
		{
			label: '升级',
			click: function () {
				console.log('update')
			}
		},
		{
			label: '退出',
			click: function () {
				if (process.platform === 'darwin') {
					// ios
					console.log('quit')
					app.quit();
				} else {
					// window
					console.log('exit')
					app.exit();
				}
			}
		}
	]);

	iconTray.setContextMenu(trayMenu);
	iconTray.setToolTip('electron应用');

//点击右上角关闭按钮隐藏任务栏图标
	const win = (BrowserWindow.getFocusedWindow() as any)

// 监听点击上面关闭
	win.on("close", (e: any) => {
		console.log('on close')
		// 调试时候先强行关闭
		// app.exit();

		if (win.isFocused()) {
			//阻止默认行为
			console.log('窗口在显示,执行隐藏')
			e.preventDefault();
			win.hide();
		}
	})

//监听托盘双击的事件
	iconTray.on("double-click", () => {
		win.show();
	})
}

export {
	initTray
};
